﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Data;
using System.Data.Entity;
using HdipCricketClub.Models;
using HdipCricketClub.ViewModels;

namespace HdipCricketClub.Controllers
{
    public class ScorecardCalledOffFixtureController : Controller
    {
        // database context

        CricketClubEntities db = new CricketClubEntities();

        // Repositories

        FixtureRepository fixtureRepository = new FixtureRepository();
        ScorecardHeaderRepository scorecardHeaderRepository = new ScorecardHeaderRepository();
        ResultTypeRepository resultTypeRepository = new ResultTypeRepository();

        // objects

        ScorecardCalledOffFixtureViewModel scorecardCalledOffFixtureViewModel = new ScorecardCalledOffFixtureViewModel();


        //
        // GET: /ScorecardCalledOffFixture/ConfirmResult

        public ActionResult ConfirmResult(int fixtureId, int resultId)
        {
            scorecardCalledOffFixtureViewModel.ScorecardFixture = fixtureRepository.GetFixture(fixtureId);
            scorecardCalledOffFixtureViewModel.ScorecardHeaderDetail = new ScorecardHeader
                                                                     {
                                                                        fixture_id = fixtureId,
                                                                        result_id = resultId,
                                                                      };
            scorecardCalledOffFixtureViewModel.ScorecardResultType = resultTypeRepository.GetResultType(resultId);            

            return View(scorecardCalledOffFixtureViewModel);
        }

        //
        //  POST: /ScorecardCalledOffFixture/ConfirmResult

        [HttpPost]
        public ActionResult ConfirmResult(ScorecardCalledOffFixtureViewModel scorecardCalledOffFixtureViewModel)
        {
            if (ModelState.IsValid)
            {
                ScorecardHeader scorecardHeader = new ScorecardHeader();

                scorecardHeader.fixture_id = scorecardCalledOffFixtureViewModel.ScorecardFixture.fixture_id;
                scorecardHeader.result_id = scorecardCalledOffFixtureViewModel.ScorecardHeaderDetail.result_id;
                scorecardHeader.won_toss = false;
                scorecardHeader.batted_first = false;
                scorecardHeader.scorecard_entered = true;

                db.ScorecardHeaders.AddObject(scorecardHeader);
                db.SaveChanges();

                scorecardCalledOffFixtureViewModel.ScorecardFixture = fixtureRepository.GetFixture(scorecardHeader.fixture_id);
                scorecardCalledOffFixtureViewModel.ScorecardHeaderDetail = scorecardHeaderRepository.GetScorecardHeader(scorecardHeader.fixture_id);
                scorecardCalledOffFixtureViewModel.ScorecardResultType = resultTypeRepository.GetResultType(scorecardCalledOffFixtureViewModel.ScorecardHeaderDetail.result_id); 

                return View("ResultEntered", scorecardCalledOffFixtureViewModel);
            }

            return View(scorecardCalledOffFixtureViewModel);
        }

    }
}
        